<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$return=array();
$return['basic']=$this->plugin_setting('basic');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//超时未支付订单取消
	$list=pdo_getall('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>'0'));
	if($list){
		$timeout=$return['basic']['timeout']?$return['basic']['timeout']:5;
		foreach($list as $row){
			if(time()-$row['createtime']>$timeout*60){
				pdo_update('jy_yuyue_order',array('status'=>3),array('id'=>$row['id']));
			}
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	//查询进入该页面的客户所有订单
	$params=array('user_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	$order_list=pdo_getslice('jy_yuyue_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_yuyue_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_yuyue_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$serve_times=pdo_getcolumn('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$serve_times[$item['timeid']];
			if($item['status']==0){
				$item['statustitle']='预约待支付';
				$item['statusdesc']='超时未支付，订单将自动取消';
			}elseif($item['status']==1){
				$item['statustitle']='预约成功';
				$item['statusdesc']='请您留意订单进度';
			}elseif($item['status']==2){
				$item['statustitle']='预约已确认';
				$item['statusdesc']='欢迎下次光临!';
			}elseif($item['status']==3){
				$item['statustitle']='预约已取消';
				$item['statusdesc']='欢迎下次光临！';
			}elseif($item['status']==4){
				$item['statustitle']='预约已完成';
				$item['statusdesc']='欢迎下次光临！';
			}elseif($item['status']==5){
				$item['statustitle']='预约退款中';
				$item['statusdesc']='请您留意退款进度';
			}elseif($item['status']==6){
				$item['statustitle']='预约已退款';
				$item['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
			}else{
				$item['statustitle']='预约已拒绝退款';
				$item['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
			}
		}
	}
	$return['list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$user=pdo_get('jy_yuyue_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$serve=pdo_get('jy_yuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));	
	$serve_times=json_decode($serve['serve_time'],true);
	$order['serve_time']=$serve_times[$order['timeid']];
	$order['serve_image']=$serve['image'];
	//技师
	$worker=pdo_get('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['worker_id']));	
	//自定义字段数据
	$fileds=$order['fileds']?json_decode($order['fileds'],true):array();
	foreach($fileds as &$item){
	    if($item['data'] && $item['type']==5){
			$item['data']=rtrim($item['data'], ";");
			$item['data']=ltrim($item['data'], ";");
		}
	}
	if($order['selfnumber']){
		$order['selfnumberurl']=custom_url('jy_yuyue/worker_hexiao_detail//id='.$order['id'].'&selfnumber='.$order['selfnumber'],true);
		//小程序二维码：
		$url='/jy_yuyue/worker_hexiao_detail/index?id='.$order['id'].'&selfnumber='.$order['selfnumber'];
		$pid='verifyid'.$order['id'].'c'.$order['selfnumber'];
		$order['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
	}
	//定义订单状态名称
	if($order['status']==0){
		$order['statustitle']='预约待支付';
		$order['statusdesc']='超时未支付，订单将自动取消';
	}elseif($order['status']==1){
		$order['statustitle']='预约成功';
		$order['statusdesc']='请您留意订单进度';
	}elseif($order['status']==2){
		$order['statustitle']='预约已确认';
		$order['statusdesc']='欢迎下次光临!';
	}elseif($order['status']==3){
		$order['statustitle']='预约已取消';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==4){
		$order['statustitle']='预约已完成';
		$order['statusdesc']='欢迎下次光临！';
	}elseif($order['status']==5){
		$order['statustitle']='预约退款中';
		$order['statusdesc']='请您留意退款进度';
	}elseif($order['status']==6){
		$order['statustitle']='预约已退款';
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}else{
		$order['statustitle']='预约已拒绝退款';
		$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
	}
	$order['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$order['id'],'plugin'=>'jy_yuyue'),'pay_type');
	$order['paytype']=get_pay_types()[$order['paytype']];
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	$return['worker']=$worker;
	$return['fileds']=$fileds;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_yuyue_order',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'worker_id');
	$pay=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'pay');
	$lev_id=pdo_getcolumn('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev');
	$rate=pdo_getcolumn('jy_yuyue_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	if($rate){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'worker_id'=>$worker_id,
			'order_id'=>$_RQ['id'],
			'money'=>$pay*$rate/100,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yuyue_commission_log',$insert);
		$commission=pdo_getcolumn('jy_yuyue_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_yuyue_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	}	
	exi('确认完成服务成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_yuyue_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	
	$order=pdo_get('jy_yuyue_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$order['package']=json_decode($order['package'],true);
	
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	pdo_update('jy_yuyue_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_yuyue/order/list/status=1');
}